/* Collision search program for Tangle
 *
 * Author: Soeren S. Thomsen<crypto@znoren.dk>
 *
 * To be compiled with the reference implementation of the Tangle
 * submission package.
 * Assumes that an unsigned int is 32 bits.
 */

#include "tangle.h"
#include <stdio.h>


#define HASHBITLEN 256

int main() {
  int i;
  unsigned char out1[128];
  unsigned char out2[128];
  unsigned int ctr = 0;

  unsigned int msg32[10];
  unsigned char *msg = (unsigned char*)msg32;

  while (++ctr < 0xffffffff) {
    for (i = 0; i < 10; i++) {
      msg32[i] = 0;
    }
    msg32[0] = ctr;
    
    Hash(HASHBITLEN, msg, 10*8*4, out1);
    
    msg32[0] ^= 0x80000000;
    msg32[1] ^= 0x80000000;
    msg32[8] ^= 0x80000000;
    msg32[9] ^= 0x80000000;
    
    Hash(HASHBITLEN, msg, 10*8*4, out2);

    for (i = 0; i < HASHBITLEN/8; i++) {
      if ((out1[i]^out2[i]) != 0) break;
    }
    if (i == HASHBITLEN/8) break;
  }
  if (ctr == -1) return 1;

  printf("Collision found in Tangle-%d\n\n", HASHBITLEN);

  msg32[0] ^= 0x80000000;
  msg32[1] ^= 0x80000000;
  msg32[8] ^= 0x80000000;
  msg32[9] ^= 0x80000000;

  printf("Message 1:\n");
  for (i = 0; i < 40; i++) printf("%02x", msg[i]);
  printf("\nHash of message 1:\n");
  for (i = 0; i < HASHBITLEN/8; i++) printf("%02x", out1[i]);
  printf("\n");
    
  msg32[0] ^= 0x80000000;
  msg32[1] ^= 0x80000000;
  msg32[8] ^= 0x80000000;
  msg32[9] ^= 0x80000000;
  
  printf("Message 2:\n");
  for (i = 0; i < 40; i++) printf("%02x", msg[i]);
  printf("\nHash of message 2:\n");
  for (i = 0; i < HASHBITLEN/8; i++) printf("%02x", out2[i]);
  printf("\n");

  printf("XOR of hashes:\n");
  for (i = 0; i < HASHBITLEN/8; i++) printf("%02x", out1[i]^out2[i]);
  printf("\n");

  return 0;
}
